package com.service;

import com.bean.Post;
import com.config.MinioAddr;
import com.mapper.PostMapper;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bean.User;
import com.mapper.UserMapper;
import com.service.UserService;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    PostMapper postMapper;


    @Override
    public Map<String, Object> getUserInfoById(int id) {
        User user = userMapper.selectById(id);

        // 我的帖子 posts 更新
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", user.getId());
        List<Post> postList = postMapper.selectByMap(map);
        user.setPosts(postList.size());

        Map<String, Object> userMap = new HashMap<>();
        userMap.put("id", user.getId());
        userMap.put("icon", MinioAddr.IMG_PATH + user.getIcon());
        userMap.put("nickName", user.getNickName());
        userMap.put("phoneNumber", user.getPhoneNumber());
        userMap.put("sign", user.getSign());
        userMap.put("antPower", user.getAntPower());
        userMap.put("myPay", user.getMyPay());
        userMap.put("mySell", user.getMySell());
        userMap.put("favorities", user.getFavorities());
        userMap.put("records", user.getRecords());
        userMap.put("posts", user.getPosts());

        // 更新用户信息
        userMapper.updateById(user);

        return userMap;
    }

    @Override
    public Map<String, Object> getSimpleUserInfoById(int id) {
        User user = userMapper.selectById(id);

        Map<String, Object> userMap = new HashMap<>();
        userMap.put("id", user.getId());
        userMap.put("icon", MinioAddr.IMG_PATH + user.getIcon());
        userMap.put("nickName", user.getNickName());
        userMap.put("sign", user.getSign());

        return userMap;
    }

    @Override
    public int getUserIdByMap(Map<String, Object> map) {
        List<User> users = userMapper.selectByMap(map);
        if (users.size() != 1) {
            return 0;
        }

        return users.get(0).getId();
    }

    @Override
    public int zhuCeUser(User user) {
        return userMapper.insert(user);
    }

}

